package com.example.shiro.utils;

import com.example.sys.org.model.SysOrgConstant;
import com.example.sys.org.model.SysOrgElement;
import com.example.sys.org.model.SysOrgRole;
import com.example.sys.org.model.SysOrgUser;
import com.example.utils.StringUtils;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

@Data
public class UserAuthInfo {

    Logger logger = LoggerFactory.getLogger(getClass());

    private SysOrgUser sysOrgUser;
    private List<String> authOrgIds = new ArrayList<>();
    private List<String> authRoleIds = new ArrayList<>();
    private Set<String> authRoleAliases = new HashSet<>();

    public UserAuthInfo(SysOrgUser sysOrgUser) {
        this.sysOrgUser = sysOrgUser;
        if (this.sysOrgUser.getFdParent() != null) {
            SysOrgElement parent = this.sysOrgUser.getFdParent();
            if (parent != null && !StringUtils.isEmpty(parent.getFdHierarchyId())) {
                String fdHierarchyId = parent.getFdHierarchyId();
                this.authOrgIds = Arrays.asList(fdHierarchyId.split(SysOrgConstant.PREF));
            }
        }
        if (!this.sysOrgUser.getFdRoles().isEmpty()) {
            List<SysOrgRole> sysOrgRoles = this.sysOrgUser.getFdRoles();
            for (SysOrgRole sysOrgRole : sysOrgRoles) {
                this.authRoleIds.add(sysOrgRole.getFdId());
                this.authRoleAliases.add(sysOrgRole.getFdCode());
            }
        }
    }

    public List<String> getUserAuthId() {
        List<String> list = new ArrayList<>();
        list.addAll(this.authOrgIds);
        list.addAll(this.authRoleAliases);
        return list;
    }
}
